﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.UserControls;

    public class StatementRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15611;
        protected GridView gvStatement;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblAllLastInvoiceAmount;
        protected Label lblAllLastInvoiceAmountText;
        protected Label lblAllLastPayAmount;
        protected Label lblAllLastPayAmountText;
        protected Label lblAllLastTotalAmount;
        protected Label lblAllLastTotalAmountText;
        protected Label lblAllPayAmount;
        protected Label lblAllPayAmountText;
        protected Label lblAllTotalAmount;
        protected Label lblAllTotalAmountText;
        protected Label lblMsg;
        protected Label lblStatementList;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal totalAmount;
        private decimal totalLastInvoiceAmount;
        private decimal totalLastPayAmount;
        private decimal totalLastTotalAmount;
        private decimal totalPayAmouont;
        protected UpdatePanel up1;

        private void BindData2StatementList(StatementRptQueryEntity statementRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(statementRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetStatementRptDataSet(statementRptQueryEntity);
            this.gvStatement.Visible = true;
            base.BindData2GridView(this.gvStatement, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            StatementRptQueryEntity statementRptQueryEntity = this.GetStatementQueryEntity();
            statementRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = statementRptQueryEntity.CurrentPage;
            statementRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetStatementRptDataSet(statementRptQueryEntity))
                {
                    base.Export2Excel("StatementRpt.xls", "15611", this.gvStatement, ds);
                }
            }
            finally
            {
                statementRptQueryEntity.IsGetAll = false;
                statementRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetStatementQueryEntity(statementRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            StatementRptQueryEntity statementRptQueryEntity = this.GetStatementQueryEntity();
            statementRptQueryEntity.CurrentPage = "0";
            this.BindData2StatementList(statementRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            StatementRptQueryEntity statementRptQueryEntity = this.GetStatementQueryEntity();
            statementRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2StatementList(statementRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            StatementRptQueryEntity statementRptQueryEntity = this.GetStatementQueryEntity();
            statementRptQueryEntity.OwnerID = "";
            statementRptQueryEntity.StatementDate = "";
            this.BindData2StatementList(statementRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            StatementRptQueryEntity statementRptQueryEntity = this.GetStatementQueryEntity();
            if (int.Parse(statementRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                statementRptQueryEntity.CurrentPage = (int.Parse(statementRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2StatementList(statementRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            StatementRptQueryEntity statementRptQueryEntity = this.GetStatementQueryEntity();
            if (int.Parse(statementRptQueryEntity.CurrentPage) > 0)
            {
                statementRptQueryEntity.CurrentPage = (int.Parse(statementRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2StatementList(statementRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            StatementRptQueryEntity statementRptQueryEntity = this.GetStatementQueryEntity();
            this.BindData2StatementList(statementRptQueryEntity);
        }

        private StatementRptQueryEntity GetStatementQueryEntity()
        {
            return (base.PageQueryEntitySession as StatementRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                StatementRptQueryEntity statementRptQueryEntity = this.GetStatementQueryEntity();
                statementRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2StatementList(statementRptQueryEntity);
            }
        }

        protected void gvStatement_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15611", this.gvStatement);
        }

        protected void gvStatement_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblCompany"));
                }
                if ((((Label) e.Row.FindControl("lblTotalAmount")) != null) && !string.IsNullOrEmpty(((Label) e.Row.FindControl("lblTotalAmount")).Text))
                {
                    this.totalAmount += decimal.Parse(((Label) e.Row.FindControl("lblTotalAmount")).Text);
                }
                if ((((Label) e.Row.FindControl("lblPayAmount")) != null) && !string.IsNullOrEmpty(((Label) e.Row.FindControl("lblPayAmount")).Text))
                {
                    this.totalPayAmouont += decimal.Parse(((Label) e.Row.FindControl("lblPayAmount")).Text);
                }
                if ((((Label) e.Row.FindControl("lblLastPayAmount")) != null) && !string.IsNullOrEmpty(((Label) e.Row.FindControl("lblLastPayAmount")).Text))
                {
                    this.totalLastPayAmount += decimal.Parse(((Label) e.Row.FindControl("lblLastPayAmount")).Text);
                }
                if ((((Label) e.Row.FindControl("lblLastInvoiceAmount")) != null) && !string.IsNullOrEmpty(((Label) e.Row.FindControl("lblLastInvoiceAmount")).Text))
                {
                    this.totalLastInvoiceAmount += decimal.Parse(((Label) e.Row.FindControl("lblLastInvoiceAmount")).Text);
                }
                if ((((Label) e.Row.FindControl("lblLastTotalAmount")) != null) && !string.IsNullOrEmpty(((Label) e.Row.FindControl("lblLastTotalAmount")).Text))
                {
                    this.totalLastTotalAmount += decimal.Parse(((Label) e.Row.FindControl("lblLastTotalAmount")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblAllTotalAmountText.Text = double.Parse(this.totalAmount.ToString()).ToString();
                this.lblAllPayAmountText.Text = double.Parse(this.totalPayAmouont.ToString()).ToString();
                this.lblAllLastPayAmountText.Text = double.Parse(this.totalLastPayAmount.ToString()).ToString();
                this.lblAllLastInvoiceAmountText.Text = double.Parse(this.totalLastInvoiceAmount.ToString()).ToString();
                this.lblAllLastTotalAmountText.Text = double.Parse(this.totalLastTotalAmount.ToString()).ToString();
            }
        }

        protected void gvStatement_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                StatementRptQueryEntity statementRptQueryEntity = this.GetStatementQueryEntity();
                statementRptQueryEntity.SortField = e.SortExpression;
                if (statementRptQueryEntity.SortDirection == "ASC")
                {
                    statementRptQueryEntity.SortDirection = "DESC";
                }
                else if (statementRptQueryEntity.SortDirection == "DESC")
                {
                    statementRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2StatementList(statementRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnAmountCancle_Click(object sender, EventArgs e)
        {
        }

        protected void lnkbtnAmountSubmit_Click(object sender, EventArgs e)
        {
        }

        protected void lnkbtnInvoiceCancle_Click(object sender, EventArgs e)
        {
        }

        protected void lnkbtnInvoiceSubmit_Click(object sender, EventArgs e)
        {
        }

        private void LoadData()
        {
            StatementRptQueryEntity statementRptQueryEntity = new StatementRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    statementRptQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    statementRptQueryEntity.StatementDate = this.PreviousPage.StatementDate;
                }
                else
                {
                    statementRptQueryEntity.OwnerID = "";
                    statementRptQueryEntity.StatementDate = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            statementRptQueryEntity.IsGetAll = false;
            statementRptQueryEntity.CurrentPage = "0";
            statementRptQueryEntity.PageSize = this.gvStatement.PageSize.ToString();
            statementRptQueryEntity.SortField = "A.STATEMENT_DATE,A.OWNER_ID";
            statementRptQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvStatement.PageSize;
            this.SetStatementQueryEntity(statementRptQueryEntity);
            this.BindData2StatementList(statementRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvStatement);
                this.LoadData();
            }
        }

        private void SetStatementQueryEntity(StatementRptQueryEntity statementRptQueryEntity)
        {
            base.PageQueryEntitySession = statementRptQueryEntity;
        }

        public StatementRptQuery PreviousPage
        {
            get
            {
                return (StatementRptQuery) base.PreviousPage;
            }
        }
    }
}

